$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + '/eslrefreshresult/list',
        datatype: "json",
        colModel: [
            // { label: 'id', name: 'id', index: 'id', width: 50, key: true },
            {label: 'SID', name: 'sid', index: 'sid', width: 60},
            {label: 'ESL_ID', name: 'eslId', index: 'esl_id', width: 60},
            // { label: '基站与价签之间通信的能量值', name: 'rpfPower', index: 'rpf_power', width: 80 },
            {label: '基站id', name: 'apId', index: 'ap_id', width: 60},
            {label: '基站mac', name: 'apMac', index: 'ap_mac', width: 80},
            {
                label: '刷新效果', name: 'eslImage', index: 'esl_image', width: 80,
                formatter: function (value, options, row) {
                    // return '<button class="btn btn-primary" οnclick="base64ToPic(" + row.eslImage + ")">查看</button>';

                    return '<input class="btn btn-info btn-xs" type="button" value="查看" onclick="vm.viewImage(\'' + row.eslImage + '\')">';

                    // return '<input class="btn btn-info btn-xs" type="button" value="查看详细" onclick="vm.detail(\''+dataUpUuid+'\')">'
                    // return '<a class="btn btn-primary" οnclick="vm.base64ToPic"><i class="fa fa-plus"></i>&nbsp;新增</a>';
                }
            },


            // formatter: function (cellvalue, options, rowObject) {
            //     return "<button class='btn btn-primary ' οnclick= \" base64ToPic (" + cellvalue.eslImage + ") \" >查看</button>" ;
            // }},

            // { label: '此价签在此基站上的最后一次心跳时间', name: 'lastHbTime', index: 'last_hb_time', width: 80 },
            // { label: '通信ACK值', name: 'lastEslAck', index: 'last_esl_ack', width: 80 },
            // { label: '最后一次通信任务类型', name: 'lastPayloadType', index: 'last_payload_type', width: 80 },
            // { label: '此价签绑定的salesNo', name: 'salesNo', index: 'sales_no', width: 80 },
            {label: '错误代码', name: 'statusNo', index: 'status_no', width: 60},
            {label: '错误信息', name: 'errMsg', index: 'err_msg', width: 80},
            {label: '重试次数', name: 'retry', index: 'retry', width: 60},
            // { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
            {label: '更新时间', name: 'updateTime', index: 'update_time', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        eslRefreshResult: {},
        showPic: false
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            console.log("add....")
            vm.showList = false;
            vm.title = "新增";
            vm.eslRefreshResult = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.eslRefreshResult.id == null ? "/eslrefreshresult/save" : "/eslrefreshresult/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.eslRefreshResult),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "/eslrefreshresult/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "/eslrefreshresult/info/" + id, function (r) {
                vm.eslRefreshResult = r.eslRefreshResult;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        },

        base64ToPic: function (base64_string) {
            vm.showPic = true
            // console.log(base64_string)

            var img = document.createElement("img");
            // added `width` , `height` properties to `img` attributes
            img.width = "250px";
            img.height = "250px";
            img.src = "data:image/png;base64," + base64_string;
            var preview = document.getElementById("img_preview");
            preview.appendChild(img);
        },

        viewImage: function (imagePath) {

            var img = document.createElement("img");
            // added `width` , `height` properties to `img` attributes
            img.width = "250px";
            img.height = "250px";
            img.src = "data:image/png;base64," + imagePath;
            var viewer = new Viewer(img, {
                hidden: function () {
                    viewer.destroy();
                },
            });
            viewer.show();
        },
    }
});